package org.example.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.example.dto.CategoryDto;
import org.example.dto.SubCategoryDto;
import org.example.entity.SysCategory;
import org.example.entity.SysDept;
import org.example.entity.SysSubCategory;
import org.example.entity.SysUser;
import org.example.mapper.SysCategoryMapper;
import org.example.mapper.SysDeptMapper;
import org.example.model.PageBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class SysCategoryService {



        @Resource
        private SysCategoryMapper sysCategoryMapper;


        public PageBean<SysCategory> getAll(Integer page) {
            Page<SysCategory> sysCategories = PageHelper.startPage(page, 5);
            PageBean<SysCategory> pageBean = new PageBean<>();
            System.out.println("sysCategories:"+sysCategories);
            pageBean.setData(sysCategoryMapper.getCategoryData());
            System.out.println("xxxxxxxxx"+PageBean.<SysCategory>builder()
                    .data(sysCategories.getResult())
                    .total(sysCategories.getTotal())
                    .current(page)  // 当前页
                    .build());

//            sysCategories.set
            return PageBean.<SysCategory>builder()
                    .data(sysCategories.getResult())
                    .total(sysCategories.getTotal())
                    .current(sysCategories.getPageNum())  // 当前页
                    .build();
        }

    public PageBean<SysSubCategory> getAllById(Integer id, Integer page) {

        System.out.println("pagexx--------xxx"+page);
        Page<SysSubCategory> sysSubCategories = PageHelper.startPage(page, 5);
        PageBean<SysSubCategory> pageBean = new PageBean<>();
        pageBean.setData(sysCategoryMapper.getSubCategoryData(id));
        System.out.println("ssssssss"+PageBean.<SysSubCategory>builder()
                .data(sysSubCategories.getResult())
                .total(sysSubCategories.getTotal())
                .current(page)  // 当前页
                .build());

        //为何报错，模拟上面那个方法，理论一样啊
        return PageBean.<SysSubCategory>builder()
                .data(pageBean.getData())
                .total(sysSubCategories.getTotal())
                .current(sysSubCategories.getPageNum())  // 当前页
                .build();
//        return sysCategoryMapper.getSubCategoryData(id);

    }

    //添加子分类
    public void addCategory(CategoryDto categoryDto){
        sysCategoryMapper.insert(categoryDto);
    }

    //更具id查找子种类信息编辑页面回显
    public SysCategory getById(Integer id){
        return sysCategoryMapper.selectById(id);
    }

    //编辑种类
    public  void editCategory(CategoryDto categoryDto){

        sysCategoryMapper.updateById(categoryDto);
    }

    public void deleteById(Integer id){
        // deleteById() 一定通过 @TableId 来表示谁是id

        sysCategoryMapper.deleteById(id);
    }
}
